﻿// Imports
using System;
using System.IO;
using System.Web.UI;

namespace Nickel
{

    /// <summary>
    /// Base class for page
    /// </summary>
    public abstract class Page : System.Web.UI.Page
    {

        /// <summary>
        /// Site URL
        /// </summary>
        protected string strBaseUrl;

        /// <summary>
        /// Data path
        /// </summary>
        protected string strDataPath = @"~/App_Data/Data.json";

        /// <summary>
        /// The site data
        /// </summary>
        public Blog Data { get; set; }

        /// <summary>
        /// Incoming argument
        /// </summary>
        protected string strArgument { get; set; }

        /// <summary>
        /// Page title
        /// </summary>
        public string strPageTitle { get; set; }

        /// <summary>
        /// Error string
        /// </summary>
        public string strError { get; set; }

        /// <summary>
        /// Meta description
        /// </summary>
        public string strMetaDescription { get; set; }

        /// <summary>
        /// Meta keywords
        /// </summary>
        public string strMetaKeywords { get; set; }

        /// <summary>
        /// Handles page initialization
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        protected virtual void Page_Init(object sender, EventArgs e)
        {

            // Initialize error string
            strError = string.Empty;

            // Define base URL
            strBaseUrl = "http://" + Request.ServerVariables["SERVER_NAME"] + "/";
            Markup.strBaseUrl = strBaseUrl;

            // Update data path
            strDataPath = Server.MapPath(strDataPath);

            // Load blog data
            Data = new Blog();
            Data.Load(strDataPath);

            // No data
            if (Data == null) 
                strError = "Unable to load data";

        }

        /// <summary>
        /// Handles page unload
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        protected void Page_Unload(object sender, EventArgs e)
        {

            // Cheack data object
            if (Data != null)
            {

                // Clean up
                Data.Dispose();
                Data = null;

            }

        }

        /// <summary>
        /// Gets profile image to attach to comments
        /// </summary>
        /// <param name="comment">The comment in question</param>
        /// <returns>img tag or empty string</returns>
        public string GetCommentProfileImage(Comment comment)
        {

            // Return string
            return Settings.PROFILE_IMAGE_ENABLED && File.Exists(Server.MapPath(@"~/uploads/profile/" + comment.Author + ".jpg")) ? "<img src=\"" + strBaseUrl + "uploads/profile/" + comment.Author + ".jpg\" align=\"left\" style=\"margin-right:10px;\" width=\"20\" height=\"20\" />" : "&nbsp; ";

        }

    }

}
